// ContentService.java
package com.search_wheel.service;

import com.search_wheel.mapper.ContentMapper;
import com.search_wheel.pojo.entity.Content;
import com.search_wheel.pojo.vo.SearchResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.List;

@Service
public class ContentService {

    @Autowired
    private ContentMapper contentMapper;

    /**
     * 搜索内容
     */
    public SearchResult<Content> searchContent(String keyword, int page, int size,String sort) {
        if (!StringUtils.hasText(keyword)) {
            return createEmptyResult(keyword, page, size, "content");
        }

        // 计算偏移量
        int offset = (page - 1) * size;

        // 查询数据和总数
        List<Content> contents = contentMapper.searchByKeyword(keyword, offset, size,sort);
        int total = contentMapper.countByKeyword(keyword);

        return createSearchResult(contents, total, page, size, keyword, "content");
    }

    /**
     * 根据作者搜索内容
     */
    public SearchResult<Content> searchByAuthor(Integer authorId, int page, int size) {
        int offset = (page - 1) * size;
        List<Content> contents = contentMapper.findByAuthorId(authorId, offset, size);

        // 这里简化处理，实际可能需要单独的count方法
        return createSearchResult(contents, contents.size(), page, size, "author:" + authorId, "content");
    }

    /**
     * 获取内容详情（并增加浏览量）
     */
    public Content getContentDetail(Integer id) {
        if (id == null) {
            return null;
        }

        Content content = contentMapper.findById(id);
        if (content != null && content.getStatus() == 1) {
            // 异步增加浏览量，避免影响查询性能
            contentMapper.incrementViewCount(id);
        }

        return content;
    }

    /**
     * 获取热门内容
     */
    public List<Content> getPopularContents(int limit) {
        return contentMapper.findByStatus((short) 1, 0, limit);
    }

    private <T> SearchResult<T> createEmptyResult(String keyword, int page, int size, String type) {
        SearchResult<T> result = new SearchResult<>();
        result.setData(List.of());
        result.setTotal(0);
        result.setPage(page);
        result.setSize(size);
        result.setKeyword(keyword);
        result.setType(type);
        return result;
    }

    private <T> SearchResult<T> createSearchResult(List<T> data, int total, int page, int size, String keyword, String type) {
        SearchResult<T> result = new SearchResult<>();
        result.setData(data);
        result.setTotal(total);
        result.setPage(page);
        result.setSize(size);
        result.setKeyword(keyword);
        result.setType(type);
        return result;
    }
    // ContentService.java
    public List<String> getContentTitlesContaining(String keyword, int limit) {
        return contentMapper.findTitlesContaining(keyword, limit);
    }


    /**
     * 获取所有内容标题（用于构建搜索词典）
     */
    public List<String> getAllContentTitles() {
        return contentMapper.findAllTitles();
    }
}
